#include<stdio.h>
#include<string.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
void sig_cld(int signal);
int main(int argc,char **argv)
{
	pid_t          pid;

	if(signal(SIGCLD,sig_cld) ==SIG_ERR)
		printf("main: signal error\n");
	if((pid = fork()) < 0)
		printf("fork error\n");
	else if(pid == 0)
	{
		printf("child pid = %d\n",getpid());
		sleep(2);
		_exit(0);
	}

	pause();
	exit(0);
}

void sig_cld(int signo)
{
	pid_t      pid;
	int        status;

	printf("SIGCLD received\n");

	if(signal(SIGCLD,sig_cld) == SIG_ERR)
		printf("sig_cld: signal error\n");
	
	if((pid = wait(&status)) < 0)
		printf("wait error\n");

	printf("pid = %d\n",pid);
	
}
